package com.wrupple.muba.desktop.server.presentation.seo;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.View;

import com.wrupple.muba.desktop.server.bussiness.seo.HTMLHelper;
import com.wrupple.muba.desktop.server.bussiness.seo.SEORewritingService;
import com.wrupple.muba.desktop.server.integration.DesktopController;

public class SEOCrawlableView implements View {
	private SEORewritingService service;
	private DesktopController desktopController;

	@Override
	public String getContentType() {
		return "text/html;charset=UTF-8";
	}

	@Override
	public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(this.getContentType());
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();

		if (desktopController == null) {
			throw new NullPointerException("Properties service and desktopController must be properly set");
		}

		HTMLHelper.createHeader(desktopController.getDesktopPageTitle(), out);
		HTMLHelper.startBody(out);
		if (service != null) {
			service.writeContent(request.getRequestURL(), request.getParameterMap(), out);
		}
		HTMLHelper.closeBody(out);
	}

	/**
	 * @param service
	 *            the service to set
	 */
	@Autowired
	public void setService(SEORewritingService service) {
		this.service = service;
	}

	@Autowired
	public void setDesktopController(DesktopController desktopController) {
		this.desktopController = desktopController;
	}

}
